﻿using System;
using System.Collections.Generic;
using System.Data;
using SystemControllers;
using eTMS.Controllers.DiemMonHocService;
using eTMS.Controllers.GiayToSinhVienService;
using eTMS.Controllers.KhoaHocService;
using eTMS.Controllers.LopService;
using eTMS.Controllers.LopSinhVienService;
using eTMS.Controllers.LyLichSinhVienService;
using eTMS.Controllers.NganhHocService;
using eTMS.Controllers.PhanNganhSinhVienService;
using eTMS.Controllers.SinhVienHocKyService;
using eTMS.Controllers.SinhVienLopTinChiService;
using eTMS.Controllers.SinhVienMonHocService;
using eTMS.Controllers.SinhVienService;
using eTMS.Controllers.SoCoSinhVienChiTietService;
using eTMS.Controllers.SoCoSinhVienService;
using eTMS.Controllers.SoNoSinhVienChiTietService;
using eTMS.Controllers.SoNoSinhVienService;
using eTMS.Controllers.LoaiDiemService;
using System.Reflection;
using eTMS.Controllers.DanTocService;
using eTMS.Controllers.TonGiaoService;
using eTMS.Controllers.KhuVucService;

namespace eTMS.Controllers
{
    public class SinhVienController : SinhVienControllerBase
    {
        public SinhVienController()
            : base()
        {
        }
        public void ResetTable()
        {
            Table = dao.SelectAllDSFilter("0=1").Tables[0];
        }
        public bool SaveSinhVienTable()
        {
            bool result = true;
            foreach (DataRow row in Table.Rows)
            {
                SinhVienObject = MapDataRowToObject(row);
                if (SinhVienObject.SinhVien_ID == 0)
                {
                    if (AddNew())
                    {
                        SavePhanNganh(SinhVienObject.SinhVien_ID, ConvertInt(row["KhoaHoc_ID"]), ConvertInt(row["NganhHoc_ID"]));
                        int Lop_ID = ConvertInt(row["Lop_ID"]);
                        if (Lop_ID > 0)
                        {
                            AddCurrentSinhVienToLop(Lop_ID);
                        }
                    }
                    else
                    {
                        result = false;
                    }
                }
            }
            return result;
        }
        public List<int> GetListSinhVien_ID()
        {
            List<int> lsID = new List<int>();
            foreach (DataRow r in Table.Rows)
            {
                lsID.Add(ConvertInt(r["SinhVien_Id"]));
            }
            return lsID;
        }
        #region Lop sinh vien
        private List<LopService.LopEntity> _LsLop = new List<LopEntity>();
        public List<LopService.LopEntity> LsLop
        {
            get
            {
                return _LsLop;
            }
        }
        public List<LopSinhVienService.LopSinhVienEntity> LoadAllLopSinhVien()
        {
            LopSinhVienService.LopSinhVienServiceClient lopSinhVienServiceClient = new LopSinhVienServiceClient();
            return new List<LopSinhVienEntity>(lopSinhVienServiceClient.SelectAllFilter("SinhVien_ID=" + SinhVienObject.SinhVien_ID.ToString()));
        }
        public List<int> LoadAllLop_ID()
        {
            return LoadAllLopSinhVien().ConvertAll<int>(delegate(LopSinhVienService.LopSinhVienEntity o) { return o.Lop_ID; });
        }
        public void LoadAllLop()
        {
            LopService.LopServiceClient lopServiceClient = new LopServiceClient();
            _LsLop = new List<LopEntity>(lopServiceClient.SelectAllFilter("Lop_ID IN(" + string.Join(",", LoadAllLop_ID().ConvertAll<string>(Convert.ToString).ToArray()) + ")"));
        }

        #endregion

        #region Nganh hoc sinh vien
        private List<NganhHocService.NganhHocEntity> _LsNganhHoc = new List<NganhHocService.NganhHocEntity>();
        private List<PhanNganhSinhVienService.PhanNganhSinhVienEntity> _LsPhanNganhSinhVien = new List<PhanNganhSinhVienService.PhanNganhSinhVienEntity>();
        private List<KhoaHocService.KhoaHocEntity> _LsKhoaHoc = new List<KhoaHocService.KhoaHocEntity>();
        public List<KhoaHocService.KhoaHocEntity> LsKhoaHoc
        {
            get
            {
                return _LsKhoaHoc;
            }
        }
        private List<KhoaService.KhoaEntity> _LsKhoa = new List<KhoaService.KhoaEntity>();
        public List<KhoaService.KhoaEntity> LsKhoa
        {
            get
            {
                return _LsKhoa;
            }
        }
        public List<NganhHocService.NganhHocEntity> LsNganhHoc
        {
            get
            {
                return _LsNganhHoc;
            }
        }
        public void LoadAllPhanNganhSinhVien()
        {
            if (_LsPhanNganhSinhVien.Count > 0) return;
            if (phanNganhSinhVienServiceClient == null)
                phanNganhSinhVienServiceClient = new PhanNganhSinhVienServiceClient();
            _LsPhanNganhSinhVien = new List<PhanNganhSinhVienService.PhanNganhSinhVienEntity>(phanNganhSinhVienServiceClient.SelectAllFilter("SinhVien_ID=" + SinhVienObject.SinhVien_ID.ToString()));
        }
        public List<int> LoadAllNganhHoc_ID()
        {
            LoadAllPhanNganhSinhVien();
            return _LsPhanNganhSinhVien.ConvertAll<int>(delegate(PhanNganhSinhVienService.PhanNganhSinhVienEntity o) { return o.NganhHoc_ID; });
        }
        public void LoadAllNganhHoc()
        {
            NganhHocService.NganhHocServiceClient nganhHocServiceClient = new NganhHocService.NganhHocServiceClient();
            _LsNganhHoc = new List<NganhHocService.NganhHocEntity>(nganhHocServiceClient.SelectAllFilter("NganhHoc_ID IN(" + string.Join(",", LoadAllNganhHoc_ID().ConvertAll<string>(Convert.ToString).ToArray()) + ")"));
        }
        public void LoadAllKhoaHoc()
        {
            LoadAllPhanNganhSinhVien();
            KhoaHocService.KhoaHocServiceClient khoaHocServiceClient = new KhoaHocService.KhoaHocServiceClient();
            _LsKhoaHoc = new List<KhoaHocEntity>(khoaHocServiceClient.SelectAllFilter("KhoaHoc_ID IN(" + string.Join(",", _LsPhanNganhSinhVien.ConvertAll<string>(delegate(PhanNganhSinhVienEntity oConvert) { return oConvert.KhoaHoc_ID.ToString(); }).ToArray()) + ")"));
        }
        public KhoaHocService.KhoaHocEntity LoadKhoaHocByNganh_ID(int NganhHoc_ID)
        {
            LoadAllPhanNganhSinhVien();
            KhoaHocController khoaHocServiceClient = new KhoaHocController();
            khoaHocServiceClient.LoadObject(FindPhanNganhByNganhHoc(NganhHoc_ID).KhoaHoc_ID);
            return khoaHocServiceClient.KhoaHocObject;
        }
        private PhanNganhSinhVienService.PhanNganhSinhVienEntity FindPhanNganhByNganhHoc(int NganhHoc_ID)
        {
            return _LsPhanNganhSinhVien.Find(delegate(PhanNganhSinhVienService.PhanNganhSinhVienEntity oFind) { return oFind.NganhHoc_ID.Equals(NganhHoc_ID); });
        }
        private PhanNganhSinhVienService.PhanNganhSinhVienEntity FindPhanNganhByNganhHocKhoaHoc(int NganhHoc_ID, int KhoaHoc_ID)
        {
            return _LsPhanNganhSinhVien.Find(delegate(PhanNganhSinhVienService.PhanNganhSinhVienEntity oFind) { return oFind.NganhHoc_ID.Equals(NganhHoc_ID) && oFind.KhoaHoc_ID.Equals(KhoaHoc_ID); });
        }
        public void LoadAllKhoa()
        {
            if (LsNganhHoc.Count == 0)
                LoadAllNganhHoc();
            KhoaService.KhoaServiceClient khoaServiceClient = new KhoaService.KhoaServiceClient();
            _LsKhoa = new List<KhoaService.KhoaEntity>(khoaServiceClient.SelectAllFilter("Khoa_ID IN(" + string.Join(",", _LsNganhHoc.ConvertAll<string>(delegate(NganhHocService.NganhHocEntity o) { return o.NganhHoc_ID.ToString(); }).ToArray()) + ")"));
        }
        PhanNganhSinhVienServiceClient phanNganhSinhVienServiceClient;
        public void SavePhanNganh(int NganhHoc_ID, int KhoaHoc_ID, bool IsDelExists)
        {
            if (phanNganhSinhVienServiceClient == null)
                phanNganhSinhVienServiceClient = new PhanNganhSinhVienServiceClient();
            PhanNganhSinhVienService.PhanNganhSinhVienEntity phanNganhSinhVienEntity = FindPhanNganhByNganhHocKhoaHoc(NganhHoc_ID, KhoaHoc_ID);
            if (phanNganhSinhVienEntity == null)
            {
                phanNganhSinhVienEntity = new PhanNganhSinhVienEntity();
                phanNganhSinhVienEntity.KhoaHoc_ID = KhoaHoc_ID;
                phanNganhSinhVienEntity.NganhHoc_ID = NganhHoc_ID;
                phanNganhSinhVienEntity.SinhVien_ID = SinhVienObject.SinhVien_ID;
                phanNganhSinhVienEntity.HeDaoTao_ID = HeDaoTao.HeDaoTao_ID;
                phanNganhSinhVienServiceClient.Save(phanNganhSinhVienEntity);
            }
            if (IsDelExists)
            {
                foreach (PhanNganhSinhVienEntity phanNganhSinhVien in _LsPhanNganhSinhVien)
                {
                    if ((phanNganhSinhVien.KhoaHoc_ID == KhoaHoc_ID) && (phanNganhSinhVien.NganhHoc_ID != NganhHoc_ID))
                    {
                        phanNganhSinhVienServiceClient.DeleteByID(phanNganhSinhVien.PhanNganhSinhVien_ID);
                    }
                }
            }
        }
        public void SavePhanNganh(int SinhVien_ID, int KhoaHoc_ID, int NganhHoc_ID)
        {
            if (phanNganhSinhVienServiceClient == null)
                phanNganhSinhVienServiceClient = new PhanNganhSinhVienServiceClient();
            phanNganhSinhVienServiceClient.DeleteBySinhVien_IDNganhHoc_IDKhoaHoc_ID(SinhVien_ID, NganhHoc_ID, KhoaHoc_ID);
            PhanNganhSinhVienEntity phanNganhSinhVienEntity = new PhanNganhSinhVienEntity();
            phanNganhSinhVienEntity.KhoaHoc_ID = KhoaHoc_ID;
            phanNganhSinhVienEntity.NganhHoc_ID = NganhHoc_ID;
            phanNganhSinhVienEntity.SinhVien_ID = SinhVien_ID;
            if (HeDaoTao != null)
            {
                phanNganhSinhVienEntity.HeDaoTao_ID = HeDaoTao.HeDaoTao_ID;
            }
            else
            {
                phanNganhSinhVienEntity.HeDaoTao_ID = 0;
            }
            phanNganhSinhVienServiceClient.Save(phanNganhSinhVienEntity);

        }
        public void SavePhanNganh(int SinhVien_ID, int KhoaHoc_ID, int NganhHoc_ID, int OldPhanNganh_ID)
        {
            if (phanNganhSinhVienServiceClient == null)
                phanNganhSinhVienServiceClient = new PhanNganhSinhVienServiceClient();
            PhanNganhSinhVienEntity phanNganhSinhVienEntity = new PhanNganhSinhVienEntity();
            phanNganhSinhVienEntity.KhoaHoc_ID = KhoaHoc_ID;
            phanNganhSinhVienEntity.NganhHoc_ID = NganhHoc_ID;
            phanNganhSinhVienEntity.SinhVien_ID = SinhVien_ID;
            phanNganhSinhVienEntity.PhanNganhSinhVien_ID = OldPhanNganh_ID;
            phanNganhSinhVienServiceClient.Update(phanNganhSinhVienEntity);

        }
        #endregion

        #region Giay to sinh vien
        private List<GiayToSinhVienService.GiayToSinhVienEntity> _LsGiayToSinhVien = new List<GiayToSinhVienEntity>();
        public void LoadGiayToSinhVien()
        {
            GiayToSinhVienService.GiayToSinhVienServiceClient giayToSinhVienServiceClient = new GiayToSinhVienServiceClient();
            _LsGiayToSinhVien = new List<GiayToSinhVienEntity>(giayToSinhVienServiceClient.SelectAllFilter("SinhVien_ID=" + SinhVienObject.SinhVien_ID.ToString()));
        }
        private GiayToSinhVienService.GiayToSinhVienEntity FindGiayToSinhVienByLoaiGiayTo_ID(int LoaiGiayTo_ID)
        {
            return _LsGiayToSinhVien.Find(delegate(GiayToSinhVienEntity giayToSinhVienEntity) { return giayToSinhVienEntity.LoaiGiayTo_ID.Equals(LoaiGiayTo_ID); });
        }
        public void SaveLoaiGiayTo(DataTable table)
        {
            GiayToSinhVienService.GiayToSinhVienServiceClient giayToSinhVienServiceClient = new GiayToSinhVienServiceClient();
            foreach (DataRowView row in table.DefaultView)
            {
                GiayToSinhVienService.GiayToSinhVienEntity giayToSinhVienEntity = FindGiayToSinhVienByLoaiGiayTo_ID((int)row["LoaiGiayTo_ID"]);
                if (ConvertBool(row["CHECKED"]))
                {
                    if (giayToSinhVienEntity == null)
                    {
                        giayToSinhVienEntity = new GiayToSinhVienEntity();
                    }
                    giayToSinhVienEntity.LoaiGiayTo_ID = ConvertInt(row["LoaiGiayTo_ID"]);
                    giayToSinhVienEntity.SinhVien_ID = SinhVienObject.SinhVien_ID;
                    giayToSinhVienEntity.SoLuong = ConvertInt(row["SoLuong"]);
                    if (giayToSinhVienEntity.GiayToSinhVien_ID > 0)
                        giayToSinhVienServiceClient.Update(giayToSinhVienEntity);
                    else
                        giayToSinhVienServiceClient.Save(giayToSinhVienEntity);
                }
                else
                {
                    if (giayToSinhVienEntity != null)
                        giayToSinhVienServiceClient.DeleteByID(giayToSinhVienEntity.GiayToSinhVien_ID);
                }
            }
        }
        #endregion

        #region Nop tien sinh vien
        private SoNoSinhVienService.SoNoSinhVienEntity _soNoSinhVienEntity = new SoNoSinhVienEntity();
        private List<SoNoSinhVienChiTietService.SoNoSinhVienChiTietEntity> _LsSoNoChiTietSinhVien = new List<SoNoSinhVienChiTietEntity>();
        public List<SoNoSinhVienChiTietService.SoNoSinhVienChiTietEntity> LsSoNoChiTietSinhVien
        {
            get
            {
                return _LsSoNoChiTietSinhVien;
            }
        }
        public void LoadSoNoSinhVien(int HocKy_ID, int NamHoc_ID)
        {
            if (SinhVienObject == null)
            {
                _soNoSinhVienEntity = new SoNoSinhVienEntity();
                _LsSoNoChiTietSinhVien = new List<SoNoSinhVienChiTietEntity>();
                return;
            }
            SoNoSinhVienServiceClient soNoSinhVienServiceClient = new SoNoSinhVienServiceClient();
            SoNoSinhVienEntity[] soNoSinhVienEntities = soNoSinhVienServiceClient.SelectAllFilter("HocKy_ID=" + HocKy_ID.ToString() + " AND NamHoc_ID=" + NamHoc_ID.ToString() + " AND SinhVien_ID=" + SinhVienObject.SinhVien_ID.ToString());
            if (soNoSinhVienEntities.Length == 0)
            {
                _soNoSinhVienEntity = new SoNoSinhVienEntity();
                _LsSoNoChiTietSinhVien = new List<SoNoSinhVienChiTietEntity>();
                return;
            }
            _soNoSinhVienEntity = soNoSinhVienEntities[0];
            SoNoSinhVienChiTietServiceClient soNoSinhVienChiTietServiceClient = new SoNoSinhVienChiTietServiceClient();
            _LsSoNoChiTietSinhVien = new List<SoNoSinhVienChiTietEntity>(soNoSinhVienChiTietServiceClient.SelectAllFilter("SoNoSinhVien_ID=" + _soNoSinhVienEntity.SoNoSinhVien_ID.ToString()));
        }
        public void SaveSoNoSinhVien(DataTable table, int HocKy_ID, int NamHoc_ID, string GhiChu, bool FindChecked)
        {
            SoNoSinhVienServiceClient soNoSinhVienServiceClient = new SoNoSinhVienServiceClient();
            if (_soNoSinhVienEntity == null)
                _soNoSinhVienEntity = new SoNoSinhVienEntity();
            _soNoSinhVienEntity.SinhVien_ID = SinhVienObject.SinhVien_ID;
            _soNoSinhVienEntity.NgayCN = DateTime.Now;
            _soNoSinhVienEntity.NamHoc_ID = NamHoc_ID;
            _soNoSinhVienEntity.HocKy_ID = HocKy_ID;
            _soNoSinhVienEntity.GhiChu = GhiChu;
            SoNoSinhVienChiTietServiceClient soNoSinhVienChiTietServiceClient = new SoNoSinhVienChiTietServiceClient();
            if (_soNoSinhVienEntity.SoNoSinhVien_ID <= 0)
            {
                _soNoSinhVienEntity = soNoSinhVienServiceClient.SaveRet(_soNoSinhVienEntity);
            }
            else
            {
                soNoSinhVienServiceClient.Update(_soNoSinhVienEntity);
            }
            foreach (DataRow row in table.Rows)
            {
                int KhoanTien_ID = ConvertInt(row["KhoanTien_ID"]);
                if (ConvertBool(row["CHECKED"]) || (!FindChecked))
                {
                    SoNoSinhVienChiTietEntity soNoSinhVienChiTietEntity = _LsSoNoChiTietSinhVien.Find(delegate(SoNoSinhVienChiTietEntity oFind) { return oFind.KhoanTien_ID.Equals(KhoanTien_ID); });
                    if (soNoSinhVienChiTietEntity == null)
                        soNoSinhVienChiTietEntity = new SoNoSinhVienChiTietEntity();
                    soNoSinhVienChiTietEntity.KhoanTien_ID = KhoanTien_ID;
                    soNoSinhVienChiTietEntity.SoNoSinhVien_ID = _soNoSinhVienEntity.SoNoSinhVien_ID;
                    soNoSinhVienChiTietEntity.SoTien = ConvertNumber(row["SoTien"]);
                    if (KhoanTien_ID == 0)
                    {
                        soNoSinhVienChiTietEntity.SoNoSinhVienChiTiet_ID = soNoSinhVienChiTietServiceClient.SaveRet(soNoSinhVienChiTietEntity).SoNoSinhVienChiTiet_ID;
                    }
                    else
                    {
                        soNoSinhVienChiTietServiceClient.Update(soNoSinhVienChiTietEntity);
                    }
                }
                else
                {
                    SoNoSinhVienChiTietEntity soNoSinhVienChiTietEntity = _LsSoNoChiTietSinhVien.Find(delegate(SoNoSinhVienChiTietEntity oFind) { return oFind.KhoanTien_ID.Equals(KhoanTien_ID); });
                    if (soNoSinhVienChiTietEntity != null)
                    {
                        soNoSinhVienChiTietServiceClient.DeleteByID(soNoSinhVienChiTietEntity.SoNoSinhVienChiTiet_ID);
                        _LsSoNoChiTietSinhVien.Remove(soNoSinhVienChiTietEntity);
                    }
                }
            }
        }
        private SoCoSinhVienService.SoCoSinhVienEntity _soCoSinhVienEntity = new SoCoSinhVienEntity();
        private List<SoCoSinhVienChiTietService.SoCoSinhVienChiTietEntity> _LsSoCoChiTietSinhVien = new List<SoCoSinhVienChiTietEntity>();
        public List<SoCoSinhVienChiTietService.SoCoSinhVienChiTietEntity> LsSoCoChiTietSinhVien
        {
            get
            {
                return _LsSoCoChiTietSinhVien;
            }
        }
        public void LoadSoCoSinhVien(int HocKy_ID, int NamHoc_ID, int SinhVien_ID)
        {
            if (SinhVien_ID == 0)
            {
                _soCoSinhVienEntity = new SoCoSinhVienEntity();
                _LsSoCoChiTietSinhVien = new List<SoCoSinhVienChiTietEntity>();
                return;
            }
            SoCoSinhVienServiceClient soCoSinhVienServiceClient = new SoCoSinhVienServiceClient();
            SoCoSinhVienEntity[] soCoSinhVienEntities = soCoSinhVienServiceClient.SelectAllFilter("HocKy_ID=" + HocKy_ID.ToString() + " AND NamHoc_ID=" + NamHoc_ID.ToString() + " AND SinhVien_ID=" + SinhVien_ID.ToString());
            if (soCoSinhVienEntities.Length == 0)
            {
                _soCoSinhVienEntity = new SoCoSinhVienEntity();
                _LsSoCoChiTietSinhVien = new List<SoCoSinhVienChiTietEntity>();
                return;
            }
            _soCoSinhVienEntity = soCoSinhVienEntities[0];
            SoCoSinhVienChiTietServiceClient soCoSinhVienChiTietServiceClient = new SoCoSinhVienChiTietServiceClient();
            _LsSoCoChiTietSinhVien = new List<SoCoSinhVienChiTietEntity>(soCoSinhVienChiTietServiceClient.SelectAllFilter("SoCoSinhVien_ID=" + _soCoSinhVienEntity.SoCoSinhVien_ID.ToString()));
        }
        public void LoadSoCoSinhVien(int HocKy_ID, int NamHoc_ID)
        {
            if (SinhVienObject == null)
            {
                _soCoSinhVienEntity = new SoCoSinhVienEntity();
                _LsSoCoChiTietSinhVien = new List<SoCoSinhVienChiTietEntity>();
                return;
            }
            LoadSoCoSinhVien(HocKy_ID, NamHoc_ID, SinhVienObject.SinhVien_ID);
        }
        public void ResetSoCo()
        {
            _soCoSinhVienEntity = new SoCoSinhVienEntity();
        }
        public DataTable LoadSoCoSoCoChiTietSinhVien(int SoCoSinhVien_ID)
        {
            SoCoSinhVienChiTietServiceClient wChiTiet = new SoCoSinhVienChiTietServiceClient();
            DataTable table = wChiTiet.SelectAllDSFilter("SoCoSinhVien_ID=" + SoCoSinhVien_ID.ToString()).Tables[0];
            AddIndexToTable(table);
            eTMS.Commons.Common.AddCheckedToTable(table);
            table.Columns.Add("TenKhoanTien");
            KhoanTienController.LoadAllKhoanTien(false);
            KhoanTienController khoanTienController = new KhoanTienController();
            foreach (DataRow row in table.Rows)
            {
                khoanTienController.LoadObject(ConvertInt(row["KhoanTien_ID"]));
                row["TenKhoanTien"] = khoanTienController.KhoanTienObject.TenKhoanTien;
            }
            SoCoSinhVienServiceClient wSoCo = new SoCoSinhVienServiceClient();
            _soCoSinhVienEntity = wSoCo.SelectOne(SoCoSinhVien_ID);
            return table;
        }
        public DataTable GetKhoanTienConNo(int HocKy_ID, int NamHoc_ID)
        {
            SoCoSinhVienChiTietServiceClient wSoCoChiTiet = new SoCoSinhVienChiTietServiceClient();
            SoCoSinhVienService.SoCoSinhVienServiceClient wSoCo = new SoCoSinhVienServiceClient();
            LoadSoNoSinhVien(HocKy_ID, NamHoc_ID);
            List<SoCoSinhVienService.SoCoSinhVienEntity> lsSoCo = new List<SoCoSinhVienEntity>(wSoCo.SelectAllFilter("NamHoc_ID=" + NamHoc_ID.ToString() + " AND HocKy_ID=" + HocKy_ID.ToString() + " AND SinhVien_ID=" + SinhVienObject.SinhVien_ID.ToString()));
            List<SoCoSinhVienChiTietEntity> lsSoCoChiTiet = new List<SoCoSinhVienChiTietEntity>();
            foreach (SoCoSinhVienService.SoCoSinhVienEntity oSoCo in lsSoCo)
            {
                lsSoCoChiTiet.AddRange(wSoCoChiTiet.SelectAllFilter("SoCoSinhVien_ID=" + oSoCo.SoCoSinhVien_ID.ToString()));
            }
            KhoanTienController.LoadAllKhoanTien(false);
            KhoanTienController khoanTienController = new KhoanTienController();
            DataTable tbl = new DataTable();
            tbl.Columns.Add("KhoanTien_ID", typeof(int));
            tbl.Columns.Add("TenKhoanTien");
            tbl.Columns.Add("SoTien", typeof(decimal));
            foreach (SoNoSinhVienChiTietService.SoNoSinhVienChiTietEntity oSoNo in _LsSoNoChiTietSinhVien)
            {
                List<SoCoSinhVienChiTietService.SoCoSinhVienChiTietEntity> ls = lsSoCoChiTiet.FindAll(delegate(SoCoSinhVienChiTietEntity oFind) { return oFind.KhoanTien_ID.Equals(oSoNo.KhoanTien_ID); });
                decimal tongTien = oSoNo.SoTien;
                foreach (SoCoSinhVienChiTietService.SoCoSinhVienChiTietEntity oSoCoChiTiet in ls)
                {
                    tongTien -= oSoCoChiTiet.SoTien;
                }
                if (tongTien != 0)
                {
                    DataRow r = tbl.NewRow();
                    r["SoTien"] = tongTien;
                    khoanTienController.LoadObject(oSoNo.KhoanTien_ID);
                    r["KhoanTien_ID"] = oSoNo.KhoanTien_ID;
                    r["TenKhoanTien"] = khoanTienController.KhoanTienObject.TenKhoanTien;
                    tbl.Rows.Add(r);
                }
            }
            eTMS.Commons.Common.AddCheckedToTable(tbl, true);
            return tbl;
        }
        public SoCoSinhVienEntity SoCoSinhVien
        {
            get
            {
                return _soCoSinhVienEntity;
            }
        }
        public void SaveSoCoSinhVien(DataTable table, int HocKy_ID, int NamHoc_ID, string GhiChu, byte KieuTT, bool IsDelPrev)
        {
            SoCoSinhVienServiceClient soCoSinhVienServiceClient = new SoCoSinhVienServiceClient();
            if (_soCoSinhVienEntity == null)
            {
                _soCoSinhVienEntity = new SoCoSinhVienEntity();
            }
            if (_soCoSinhVienEntity.SoCoSinhVien_ID == 0)
            {
                _soCoSinhVienEntity.SinhVien_ID = SinhVienObject.SinhVien_ID;
                _soCoSinhVienEntity.NamHoc_ID = NamHoc_ID;
                _soCoSinhVienEntity.HocKy_ID = HocKy_ID;
                _soCoSinhVienEntity.KieuThanhToan = KieuTT;
            }
            _soCoSinhVienEntity.GhiChu = GhiChu;
            _soCoSinhVienEntity.NgayNop = DateTime.Now;
            SoCoSinhVienChiTietServiceClient soCoSinhVienChiTietServiceClient = new SoCoSinhVienChiTietServiceClient();
            if (_soCoSinhVienEntity.SoCoSinhVien_ID <= 0)
            {
                _soCoSinhVienEntity = soCoSinhVienServiceClient.SaveRet(_soCoSinhVienEntity);
            }
            else
            {
                soCoSinhVienServiceClient.Update(_soCoSinhVienEntity);
                if (IsDelPrev)
                    soCoSinhVienChiTietServiceClient.DeleteBySoCoSinhVien_ID(_soCoSinhVienEntity.SoCoSinhVien_ID);
            }
            foreach (DataRow row in table.Rows)
            {
                if (ConvertBool(row["CHECKED"]))
                {
                    SoCoSinhVienChiTietEntity soCoSinhVienChiTietEntity = new SoCoSinhVienChiTietEntity();
                    soCoSinhVienChiTietEntity.KhoanTien_ID = ConvertInt(row["KhoanTien_ID"]);
                    soCoSinhVienChiTietEntity.SoCoSinhVien_ID = _soCoSinhVienEntity.SoCoSinhVien_ID;
                    soCoSinhVienChiTietEntity.SoTien = ConvertNumber(row["SoTien"]);
                    if (table.Columns.Contains("SoCoSinhVienChiTiet_ID"))
                    {
                        soCoSinhVienChiTietEntity.SoCoSinhVienChiTiet_ID = ConvertInt(row["SoCoSinhVienChiTiet_ID"]);
                    }
                    if (soCoSinhVienChiTietEntity.SoCoSinhVienChiTiet_ID > 0)
                    {
                        soCoSinhVienChiTietServiceClient.Update(soCoSinhVienChiTietEntity);
                    }
                    else
                    {
                        soCoSinhVienChiTietServiceClient.Save(soCoSinhVienChiTietEntity);
                    }
                }
            }
        }
        public void DeleteSoCoSinhVienByList(List<int> lsSoCoSinhVien)
        {
            SoCoSinhVienServiceClient w = new SoCoSinhVienServiceClient();
            foreach (int id in lsSoCoSinhVien)
            {
                w.DeleteByID(id);
            }
        }
        public void SearchSinhVienNopTien(int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID, int NamHoc_ID, int HocKy_ID, DateTime TuNgay, DateTime DenNgay, byte KieuThanhToan)
        {
            Table = dao.SearchSinhVienNopTien(Khoa_ID, NganhHoc_ID, Lop_ID, KhoaHoc_ID, SinhVienObject, NamHoc_ID, HocKy_ID, eTMS.Commons.Common.BeginDay(TuNgay).AddSeconds(-2), eTMS.Commons.Common.EndDay(DenNgay).AddSeconds(2), true, KieuThanhToan).Tables[0];
            AddIndexToTable();
            AddTenGioiTinh(Table);
        }
        public void SearchSinhVienNopTien(int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID, int NamHoc_ID, int HocKy_ID, byte KieuThanhToan)
        {
            Table = dao.SearchSinhVienNopTien(Khoa_ID, NganhHoc_ID, Lop_ID, KhoaHoc_ID, SinhVienObject, NamHoc_ID, HocKy_ID, DateTime.Now, DateTime.Now, false, KieuThanhToan).Tables[0];
            AddIndexToTable();
            AddTenGioiTinh(Table);
        }

        public void TongHopDoiTuongTroCap(int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID, int NamHoc_ID, int HocKy_ID)
        {
            Table = dao.TongHopDoiTuongTroCap(Khoa_ID, NganhHoc_ID, Lop_ID, KhoaHoc_ID, SinhVienObject, NamHoc_ID, HocKy_ID).Tables[0];
            AddIndexToTable();
            AddTenGioiTinh(Table);
            eTMS.Commons.Common.AddCheckedToTable(Table);
        }
        public void SaveLoMienGiam(int NamHoc_ID, int HocKy_ID)
        {
            SoCoSinhVienServiceClient wSoCo = new SoCoSinhVienServiceClient();
            List<int> lsSinhVien_ID = new List<int>();
            foreach (DataRow row in Table.Rows)
            {
                if (ConvertBool(row["Checked"]))
                {
                    lsSinhVien_ID.Add(ConvertInt(row["SinhVien_ID"]));
                }
            }
            if (lsSinhVien_ID.Count > 0)
            {
                if (wSoCo.DeleteByKieuThanhToanNamHocHocKyListSinhVienID(Commons.Constants.KIEUTHANHTOAN_MIENGIAM, NamHoc_ID, HocKy_ID, lsSinhVien_ID.ToArray()))
                {
                    SoCoSinhVienChiTietServiceClient wChiTiet = new SoCoSinhVienChiTietServiceClient();
                    foreach (DataRow row in Table.Rows)
                    {
                        if (ConvertBool(row["Checked"]))
                        {
                            SoCoSinhVienService.SoCoSinhVienEntity oSoCo = new SoCoSinhVienEntity();
                            oSoCo.GhiChu = row["TenLoaiDoiTuong"].ToString();
                            oSoCo.HocKy_ID = HocKy_ID;
                            oSoCo.KieuThanhToan = Commons.Constants.KIEUTHANHTOAN_MIENGIAM;
                            oSoCo.NamHoc_ID = NamHoc_ID;
                            oSoCo.NgayNop = DateTime.Now;
                            oSoCo.SinhVien_ID = ConvertInt(row["SinhVien_ID"]);
                            oSoCo = wSoCo.SaveRet(oSoCo);
                            SoCoSinhVienChiTietEntity oChiTiet = new SoCoSinhVienChiTietEntity();
                            oChiTiet.KhoanTien_ID = ConvertInt(row["KhoanTien_ID"]);
                            oChiTiet.SoCoSinhVien_ID = oSoCo.SoCoSinhVien_ID;
                            oChiTiet.SoTien = ConvertNumber(row["MienGiam"]);
                            wChiTiet.Save(oChiTiet);
                        }
                    }
                }
            }
        }
        #endregion

        #region Search sinh vien
        public void LoadSinhVienByMa(string MaSinhVien)
        {
            SinhVienObject = dao.SelectOneByMaSinhVien(MaSinhVien);
        }
        public void SearchSinhVien(int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID)
        {
            Table = dao.SearchSinhVien(Khoa_ID, NganhHoc_ID, Lop_ID, KhoaHoc_ID, SinhVienObject).Tables[0];
            Table = SortByLastName("TenSinhVien", "Ho", "Ten", Table);
            AddIndexToTable();
            AddTenGioiTinh(Table);

        }
        public void LoadSinhVienByListID(List<int> LsSinhVien_ID)
        {
            Table = dao.SelectAllDSSinhVienByListID(LsSinhVien_ID.ToArray()).Tables[0];
            Table = SortByLastName("TenSinhVien", "Ho", "Ten", Table);
            AddIndexToTable();
            AddTenGioiTinh(Table);
        }
        public void SearchSinhVienHocMonHoc(int NamHoc_ID, int HocKy_ID, int MonHoc_ID, int Lop_ID)
        {
            Table = dao.SearchSinhVienHocMonHoc(MonHoc_ID, NamHoc_ID, HocKy_ID, Lop_ID, SinhVienObject).Tables[0];
            Table = SortByLastName("TenSinhVien", "Ho", "Ten", Table);
            AddIndexToTable();
            AddTenGioiTinh(Table);

        }
        public static void AddTenGioiTinh(DataTable tbl)
        {
            if (!tbl.Columns.Contains("TENGIOITINH"))
                tbl.Columns.Add("TENGIOITINH");
            foreach (DataRow row in tbl.Rows)
            {
                row["TENGIOITINH"] = Commons.Common.ConvertNumber(row["GIOITINH"]) == 1 ? Commons.Constants.APP_MALE : Commons.Constants.APP_FEMALE;
            }
        }
        public void SearchSinhVienChuaPhanLop(int NganhHoc_ID, int KhoaHoc_ID)
        {
            Table = dao.SearchSinhVienChuaPhanLop(NganhHoc_ID, KhoaHoc_ID, SinhVienObject).Tables[0];
            Table = SortByLastName("TenSinhVien", "Ho", "Ten", Table);
            AddIndexToTable();
            AddTenGioiTinh(Table);
        }
        #endregion

        #region Phan lop sinh vien
        public void PhanLopChoSinhVien()
        {
            LopSinhVienController lopSinhVienController = new LopSinhVienController();
            foreach (DataRow row in Table.Rows)
            {
                lopSinhVienController.ResetObject();
                if (row.Table.Columns.Contains("LopSinhVien_ID"))
                    lopSinhVienController.LopSinhVienObject.LopSinhVien_ID = ConvertInt(row["LopSinhVien_ID"]);
                lopSinhVienController.LopSinhVienObject.Lop_ID = ConvertInt(row["Lop_ID"]);
                lopSinhVienController.LopSinhVienObject.SinhVien_ID = ConvertInt(row["SinhVien_ID"]);
                lopSinhVienController.LopSinhVienObject.TrangThai = 0;
                if (lopSinhVienController.LopSinhVienObject.LopSinhVien_ID == 0)
                {
                    lopSinhVienController.AddNew();
                }
                else
                {
                    lopSinhVienController.Update();
                }
            }
        }
        LopSinhVienController lopSinhVienController;
        public void AddCurrentSinhVienToLop(int Lop_ID)
        {
            if (lopSinhVienController == null)
                lopSinhVienController = new LopSinhVienController();
            lopSinhVienController.ResetObject();
            lopSinhVienController.LopSinhVienObject.Lop_ID = Lop_ID;
            lopSinhVienController.LopSinhVienObject.SinhVien_ID = SinhVienObject.SinhVien_ID;
            lopSinhVienController.LopSinhVienObject.TrangThai = 0;
            lopSinhVienController.AddNew();
        }
        public void DeleteCurrentSinhVienFromLop(int Lop_ID)
        {
            DeleteSinhVienFromLop(Lop_ID, SinhVienObject.SinhVien_ID);
        }
        public void DeleteSinhVienFromLop(int Lop_ID, int SinhVien_ID)
        {
            if (lopSinhVienController == null)
                lopSinhVienController = new LopSinhVienController();
            lopSinhVienController.DeleteByLop_IDSinhVien_ID(Lop_ID, SinhVien_ID);

        }
        public void SaveBaoLuuSinhVien(int HocKy_ID, int NamHoc_ID, DateTime ThoiHanBaoLuu,List<DataRow>LsSinhVienRows)
        {
            DanhSachBaoLuuService.DanhSachBaoLuuServiceClient wDanhSachBaoLuu = new eTMS.Controllers.DanhSachBaoLuuService.DanhSachBaoLuuServiceClient();
            if (lopSinhVienController == null)
                lopSinhVienController = new LopSinhVienController();
            foreach (DataRow r in LsSinhVienRows)
            {
                DanhSachBaoLuuService.DanhSachBaoLuuEntity oBaoLuu = new eTMS.Controllers.DanhSachBaoLuuService.DanhSachBaoLuuEntity();
                oBaoLuu.HocKy_ID = HocKy_ID;
                oBaoLuu.Lop_ID = ConvertInt(r["Lop_ID"]);
                oBaoLuu.NamHoc_ID = NamHoc_ID;
                oBaoLuu.NganhHoc_ID = ConvertInt(r["NganhHoc_ID"]);
                oBaoLuu.NgayBaoLuu = DateTime.Now;
                oBaoLuu.SinhVien_ID = ConvertInt(r["SinhVien_ID"]);
                oBaoLuu.ThoiHanBaoLuu = ThoiHanBaoLuu;
                if (wDanhSachBaoLuu.Save(oBaoLuu))
                {
                    lopSinhVienController.DeleteByLop_IDSinhVien_ID(oBaoLuu.Lop_ID, oBaoLuu.SinhVien_ID);
                }
            }
        }
        public void LoadAllDSSinhVienBaoLuu()
        {
            DanhSachBaoLuuService.DanhSachBaoLuuServiceClient w = new eTMS.Controllers.DanhSachBaoLuuService.DanhSachBaoLuuServiceClient();
            Table = w.SelectAllDSSinhVienBaoLuu().Tables[0];
            Table=SortByLastName("TenSinhVien", "Ho", "Ten", Table);
            AddIndexToTable();
        }
        #endregion

        #region Cap nhat ma sinh vien
        public bool CheckExistsMaSinhVien()
        {
            if (!Table.Columns.Contains("ExistsMa"))
                Table.Columns.Add("ExistsMa", typeof(bool));
            List<string> ls = new List<string>();
            DataTable tableCheck = null;
            foreach (DataRow row in Table.Rows)
            {
                ls.Add(row["MaSinhVien"].ToString());
                if (ls.Count >= 1000)
                {
                    DataTable tblTemp = dao.SelectAllDSFilter("SinhVien.MaSinhVien IN('" + string.Join("','", ls.ToArray()) + "')").Tables[0];
                    if (tableCheck == null)
                    {
                        tableCheck = tblTemp;
                    }
                    else
                    {
                        foreach (DataRow dataRow in tblTemp.Rows)
                        {
                            tableCheck.ImportRow(dataRow);
                        }
                    }
                    ls = new List<string>();
                }
            }
            if (ls.Count > 0)
            {
                DataTable tblTemp = dao.SelectAllDSFilter("SinhVien.MaSinhVien IN('" + string.Join("','", ls.ToArray()) + "')").Tables[0];
                if (tableCheck == null)
                {
                    tableCheck = tblTemp;
                }
                else
                {
                    foreach (DataRow dataRow in tblTemp.Rows)
                    {
                        tableCheck.ImportRow(dataRow);
                    }
                }
            }
            bool result = false;
            foreach (DataRow row in Table.Rows)
            {
                row["ExistsMa"] = false;
                DataRow[] rows = tableCheck.Select("MaSinhVien='" + row["MaSinhVien"].ToString() + "'");
                if (rows.Length > 0)
                {
                    if (Table.Select("SinhVien_ID=" + rows[0]["SinhVien_ID"].ToString()).Length == 0)
                    {
                        row["ExistsMa"] = true;
                        result = true;
                    }
                }

            }
            return result;
        }
        public bool UpdateMaSinhVien()
        {
            bool result = false;
            foreach (DataRow row in Table.Rows)
            {
                result = dao.UpdateMaSinhVien(row["MaSinhVien"].ToString(), ConvertInt(row["SinhVien_ID"]));
            }
            return result;
        }
        #endregion

        #region Ly lich sinh vien
        private LyLichSinhVienService.LyLichSinhVienEntity _lyLichSinhVienEntity;
        public void ResetLyLichSinhVien()
        {
            _lyLichSinhVienEntity = new LyLichSinhVienEntity();
        }
        public LyLichSinhVienEntity LyLichSinhVienEntity
        {
            get
            {
                return _lyLichSinhVienEntity;
            }
            set
            {
                _lyLichSinhVienEntity = value;
            }
        }
        public bool SaveLyLichSinhVien()
        {
            LyLichSinhVienService.LyLichSinhVienServiceClient lyLichSinhVienServiceClient = new LyLichSinhVienServiceClient();
            if (_lyLichSinhVienEntity.SinhVien_ID == 0)
            {
                _lyLichSinhVienEntity.SinhVien_ID = SinhVienObject.SinhVien_ID;
                return lyLichSinhVienServiceClient.Save(_lyLichSinhVienEntity);
            }
            return lyLichSinhVienServiceClient.Update(_lyLichSinhVienEntity);
        }
        public void LoadLyLichSinhVien()
        {
            LyLichSinhVienServiceClient lyLichSinhVienServiceClient = new LyLichSinhVienServiceClient();
            _lyLichSinhVienEntity = lyLichSinhVienServiceClient.SelectOne(SinhVienObject.SinhVien_ID);
        }
        public bool DeleteSinhVienByListSinhVien_ID(List<int> lsSinhVien_ID)
        {
            return dao.DeleteSinhVienByListSinhVien_ID(lsSinhVien_ID.ToArray());
        }
        #endregion

        #region Diem mon hoc
        public void SaveDiemMonHoc(int HocKy_ID, int NamHoc_ID, int LanThu, int MonHoc_ID, int LoaiDiem_ID, int NamThu)
        {
            SaveDiemMonHocListRows(Table.Select(), HocKy_ID, NamHoc_ID, LanThu, MonHoc_ID, LoaiDiem_ID, NamThu);
        }
        public void SaveDiemMonHocListRows(DataRow[] Rows, int HocKy_ID, int NamHoc_ID, int LanThu, int MonHoc_ID, int LoaiDiem_ID, int NamThu)
        {
            SaveDiemMonHocListRows(Rows, HocKy_ID, NamHoc_ID, LanThu, MonHoc_ID, LoaiDiem_ID, NamThu, "SinhVienMonHoc_ID", "DiemMonHoc_ID", "Diem");
        }
        private void SaveDiemMonHocListRows(DataRow[] Rows, int HocKy_ID, int NamHoc_ID, int LanThu, int MonHoc_ID, int LoaiDiem_ID, int NamThu, string ColSinhVienMonHoc_ID, string ColDiemMonHoc_ID, string ColDiem)
        {
            SinhVienMonHocService.SinhVienMonHocServiceClient sinhVienMonHocServiceClient = new SinhVienMonHocServiceClient();
            DiemMonHocService.DiemMonHocServiceClient diemMonHocServiceClient = new DiemMonHocServiceClient();
            List<DiemMonHocService.DiemMonHocEntity> lsDiemMonHoc = new List<DiemMonHocEntity>();
            List<SinhVienMonHocEntity> lsSinhVienMonHoc = new List<SinhVienMonHocEntity>();
            List<DataRow> lsRowSinhVien = new List<DataRow>();
            decimal scoreToPass = (decimal)GetDoubleConfig("SUBJECT_SCORE_PASS");
            foreach (DataRow row in Rows)
            {
                if (row.IsNull(ColSinhVienMonHoc_ID))
                {
                    //need to addnew
                    lsSinhVienMonHoc.Add(CreateSinhVienMonHocEntity(row, NamHoc_ID, HocKy_ID, MonHoc_ID, NamThu));
                    lsRowSinhVien.Add(row);
                }
            }
            //Save SinhVienMonHoc
            lsSinhVienMonHoc = new List<SinhVienMonHocEntity>(sinhVienMonHocServiceClient.SaveListRet(lsSinhVienMonHoc.ToArray()));
            for (int i = 0; i < lsSinhVienMonHoc.Count; i++)
            {
                //Update ID to DataRow
                lsRowSinhVien[i][ColSinhVienMonHoc_ID] = lsSinhVienMonHoc[i].SinhVienMonHoc_ID;
            }
            lsRowSinhVien = new List<DataRow>();
            List<int> lsDiemMonHoc_ID = new List<int>();
            List<Decimal> lsDiem = new List<decimal>();
            List<bool> lsSinhVienMonHocDat = new List<bool>();
            List<int> lsSinhVienMonHocID = new List<int>();
            foreach (DataRow row in Rows)
            {
                if (row.IsNull(ColDiemMonHoc_ID))
                {
                    //Addnew
                    lsDiemMonHoc.Add(CreateDiemMonHocEntity(row, NamHoc_ID, HocKy_ID, MonHoc_ID, LoaiDiem_ID, LanThu, "Diem"));
                    lsRowSinhVien.Add(row);
                    lsSinhVienMonHocDat.Add(lsDiemMonHoc[lsDiemMonHoc.Count - 1].Diem >= scoreToPass);
                    lsSinhVienMonHocID.Add(lsDiemMonHoc[lsDiemMonHoc.Count - 1].SinhVienMonHoc_ID);
                }
                else
                {
                    //Update
                    lsDiemMonHoc_ID.Add(ConvertInt(row[ColDiemMonHoc_ID]));
                    lsDiem.Add(ConvertNumber(row[ColDiem]));

                    lsSinhVienMonHocDat.Add(lsDiem[lsDiem.Count - 1] >= scoreToPass);
                    lsSinhVienMonHocID.Add(ConvertInt(row[ColSinhVienMonHoc_ID]));
                }

            }
            //Save addnew
            lsDiemMonHoc = new List<DiemMonHocEntity>(diemMonHocServiceClient.SaveListRet(lsDiemMonHoc.ToArray()));
            for (int i = 0; i < lsDiemMonHoc.Count; i++)
            {
                lsRowSinhVien[i][ColDiemMonHoc_ID] = lsDiemMonHoc[i].DiemMonHoc_ID;
            }
            //Save update
            diemMonHocServiceClient.UpdateListDiem(lsDiemMonHoc_ID.ToArray(), lsDiem.ToArray());
            //Update dat
            sinhVienMonHocServiceClient.UpdateListDat(lsSinhVienMonHocID.ToArray(), lsSinhVienMonHocDat.ToArray());

        }
        private DiemMonHocEntity CreateDiemMonHocEntity(DataRow row, int NamHoc_ID, int HocKy_ID, int MonHoc_ID, int LoaiDiem_ID, int LanThu, string colDiem)
        {
            DiemMonHocEntity diemMonHocEntity = new DiemMonHocEntity();
            diemMonHocEntity.Diem = ConvertNumber(row[colDiem]);
            diemMonHocEntity.LanThi = (byte)LanThu;
            diemMonHocEntity.MonHoc_ID = MonHoc_ID;
            diemMonHocEntity.SinhVien_ID = ConvertInt(row["SinhVien_ID"]);
            diemMonHocEntity.SinhVienMonHoc_ID = ConvertInt(row["SinhVienMonHoc_ID"]);
            diemMonHocEntity.LoaiDiem_ID = LoaiDiem_ID;
            return diemMonHocEntity;
        }
        private SinhVienMonHocEntity CreateSinhVienMonHocEntity(DataRow row, int NamHoc_ID, int HocKy_ID, int MonHoc_ID, int NamThu)
        {
            SinhVienMonHocEntity sinhVienMonHocEntity = new SinhVienMonHocEntity();
            sinhVienMonHocEntity.HocKy_ID = HocKy_ID;
            sinhVienMonHocEntity.NamHoc_ID = NamHoc_ID;
            sinhVienMonHocEntity.MonHoc_ID = MonHoc_ID;
            sinhVienMonHocEntity.SinhVien_ID = ConvertInt(row["SinhVien_ID"]);
            sinhVienMonHocEntity.NamThu = (byte)NamThu;
            sinhVienMonHocEntity.IsDat = false;
            return sinhVienMonHocEntity;
        }
        public void SearchSinhVienWithDiemMonHocByLopLoaiDiem(int Lop_ID, int HocKy_ID, int NamHoc_ID, int MonHoc_ID, int LoaiDiem_ID, int LanThi)
        {
            SinhVienEntity o = new SinhVienEntity();
            o.GioiTinh = 2;
            Table = dao.SearchSinhVienWithDiemMonHoc(true, true, false, false, MonHoc_ID, LoaiDiem_ID, LanThi, HocKy_ID, NamHoc_ID, -1, -1, Lop_ID, -1, o, "").Tables[0];
            Table = SortByLastName("TenSinhVien", "Ho", "Ten", Table);
            AddIndexToTable();
            AddTenGioiTinh(Table);
        }
        public void SearchSinhVienWithDiemMonHocByMonHoc(int Khoa_ID, int Nganh_ID, int KhoaHoc_ID, int Lop_ID, int LopTinChi_ID, int HocKy_ID, int NamHoc_ID, int MonHoc_ID, int LoaiDiem_ID, int LanThi)
        {
            SinhVienEntity o = new SinhVienEntity();
            o.GioiTinh = 2;
            string filter = "";
            if (LopTinChi_ID > 0)
            {
                filter = "SinhVienLopTinChi.LopTinChi_ID=" + LopTinChi_ID.ToString();
            }
            Table = dao.SearchSinhVienWithDiemMonHoc(true, true, true, true, MonHoc_ID, LoaiDiem_ID, LanThi, HocKy_ID, NamHoc_ID, Khoa_ID, Nganh_ID, Lop_ID, KhoaHoc_ID, o, filter).Tables[0];
            Table = SortByLastName("TenSinhVien", "Ho", "Ten", Table);
            AddIndexToTable();
            AddTenGioiTinh(Table);
        }
        public void SearchSinhVienWithSinhVienMonHocByLop(int Lop_ID, int NganhHoc_ID, int Khoa_ID, int KhoaHoc_ID, int HocKy_ID, int NamHoc_ID, int MonHoc_ID)
        {
            SinhVienEntity o = new SinhVienEntity();
            o.GioiTinh = 2;
            Table = dao.SearchSinhVienWithDiemMonHoc(false, false, true, false, MonHoc_ID, -1, 0, HocKy_ID, NamHoc_ID, Khoa_ID, NganhHoc_ID, Lop_ID, KhoaHoc_ID, o, "").Tables[0];
            Table = SortByLastName("TenSinhVien", "Ho", "Ten", Table);
            AddIndexToTable();
            AddTenGioiTinh(Table);
            Table.AcceptChanges();

        }
        public void SearchSinhVienWithAllDiemMonHocForScoreTable(int Khoa_ID, int Nganh_ID, int Lop_ID, int KhoaHoc_ID, List<int> LsSinhVien_Id)
        {
            int LoaiDiem_IDTongKet = 0;
            LoaiDiemController.LoadAllLoaiDiem(false);
            foreach (LoaiDiemEntity oLoaiDiem in LoaiDiemController.ListLoaiDiemStatic)
            {
                if (oLoaiDiem.IsDiemTongKet)
                {
                    LoaiDiem_IDTongKet = oLoaiDiem.LoaiDiem_ID;
                }
            }
            string filter = "";
            if (LsSinhVien_Id.Count > 0)
            {
                filter = "SinhVienMonHoc.SinhVien_ID IN(" + string.Join(",", LsSinhVien_Id.ConvertAll<string>(Convert.ToString).ToArray()) + ")";
            }
            SinhVienEntity o = new SinhVienEntity();
            o.GioiTinh = 2;
            DataTable tblTemp = dao.SearchSinhVienWithDiemMonHoc(false, true, true, false, -1, LoaiDiem_IDTongKet, -1, -1, -1, Khoa_ID, Nganh_ID, Lop_ID, KhoaHoc_ID, o, filter).Tables[0];

            Table = new DataTable();
            foreach (LoaiDiemEntity loaiDiemEntity in LoaiDiemController.ListLoaiDiemStatic)
            {
                Table.Columns.Add("Col_" + loaiDiemEntity.LoaiDiem_ID.ToString(), typeof(decimal));
                Table.Columns.Add("Col_diemmonhoc_id_" + loaiDiemEntity.LoaiDiem_ID.ToString(), typeof(int));
            }
            foreach (DataColumn col in tblTemp.Columns)
            {
                if ((col.ColumnName.ToLower() != "diem") || (col.ColumnName.ToLower() != "loaidiem_id") || (col.ColumnName.ToLower() != "diemmonhoc_id"))
                {
                    Table.Columns.Add(col.ColumnName, col.DataType);
                }
            }
            Dictionary<int, DataRow> dicRows = new Dictionary<int, DataRow>();
            foreach (DataRow row in tblTemp.Rows)
            {
                int sinhvien_id = ConvertInt(row["SinhVien_ID"]);
                DataRow r = null;
                if (!dicRows.ContainsKey(sinhvien_id))
                {
                    r = Table.NewRow();
                    foreach (DataColumn col in tblTemp.Columns)
                    {
                        if (Table.Columns.Contains(col.ColumnName))
                        {
                            r[col.ColumnName] = row[col.ColumnName];
                        }
                    }
                    dicRows.Add(sinhvien_id, r);
                    Table.Rows.Add(r);
                }
                else
                {
                    r = dicRows[sinhvien_id];
                }
                if (!row.IsNull("LoaiDiem_ID"))
                {
                    r["Col_" + row["LoaiDiem_ID"].ToString()] = row["Diem"];
                    r["Col_diemmonhoc_id_" + row["LoaiDiem_ID"].ToString()] = row["DiemMonHoc_ID"];
                }
            }
            AddIndexToTable();
            AddTenGioiTinh(Table);
        }

        public void SearchSinhVienWithAllDiemMonHocByLop(int Lop_ID, int HocKy_ID, int NamHoc_ID, int MonHoc_ID)
        {
            SinhVienEntity o = new SinhVienEntity();
            o.GioiTinh = 2;
            DataTable tblTemp = dao.SearchSinhVienWithDiemMonHoc(true, true, true, false, MonHoc_ID, -1, -1, HocKy_ID, NamHoc_ID, -1, -1, Lop_ID, -1, o, "").Tables[0];

            Table = new DataTable();
            foreach (LoaiDiemEntity loaiDiemEntity in LoaiDiemController.ListLoaiDiemStatic)
            {
                Table.Columns.Add("Col_" + loaiDiemEntity.LoaiDiem_ID.ToString(), typeof(decimal));
                Table.Columns.Add("Col_diemmonhoc_id_" + loaiDiemEntity.LoaiDiem_ID.ToString(), typeof(int));
            }
            foreach (DataColumn col in tblTemp.Columns)
            {
                if ((col.ColumnName.ToLower() != "diem") || (col.ColumnName.ToLower() != "loaidiem_id") || (col.ColumnName.ToLower() != "diemmonhoc_id"))
                {
                    Table.Columns.Add(col.ColumnName, col.DataType);
                }
            }
            Dictionary<int, DataRow> dicRows = new Dictionary<int, DataRow>();
            foreach (DataRow row in tblTemp.Rows)
            {
                int sinhvien_id = ConvertInt(row["SinhVien_ID"]);
                DataRow r = null;
                if (!dicRows.ContainsKey(sinhvien_id))
                {
                    r = Table.NewRow();
                    foreach (DataColumn col in tblTemp.Columns)
                    {
                        if (Table.Columns.Contains(col.ColumnName))
                        {
                            r[col.ColumnName] = row[col.ColumnName];
                        }
                    }
                    dicRows.Add(sinhvien_id, r);
                    Table.Rows.Add(r);
                }
                else
                {
                    r = dicRows[sinhvien_id];
                }
                if (!row.IsNull("LoaiDiem_ID"))
                {
                    r["Col_" + row["LoaiDiem_ID"].ToString()] = row["Diem"];
                    r["Col_diemmonhoc_id_" + row["LoaiDiem_ID"].ToString()] = row["DiemMonHoc_ID"];
                }
            }
            AddIndexToTable();
            AddTenGioiTinh(Table);
        }
        public void SaveDiemTongKetMonHoc(int HocKy_ID, int NamHoc_ID, int LanThu, int MonHoc_ID, int LoaiDiem_ID)
        {
            SaveDiemMonHocListRows(Table.Select(), HocKy_ID, NamHoc_ID, LanThu, MonHoc_ID, LoaiDiem_ID, 1, "SinhVienMonHoc_ID", "Col_diemmonhoc_id_" + LoaiDiem_ID.ToString(), "Col_" + LoaiDiem_ID.ToString());
        }
        public void SearchSinhVienWithDiemTongKetByLop(int HocKy_ID, int NamHoc_ID, int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID)
        {
            Table = dao.SearchSinhVienWithDiemTongKetHocKy(HocKy_ID, NamHoc_ID, Khoa_ID, NganhHoc_ID, Lop_ID, KhoaHoc_ID, SinhVienObject).Tables[0];
            Table = SortByLastName("TenSinhVien", "Ho", "Ten", Table);
            AddIndexToTable();
            AddTenGioiTinh(Table);

        }
        public bool UpdateIsDat()
        {
            SinhVienMonHocServiceClient wSVMH = new SinhVienMonHocServiceClient();
            List<int> lsSinhVienMonHoc_ID = new List<int>();
            List<bool> lsIsDat = new List<bool>();
            foreach (DataRow r in Table.Rows)
            {
                if (r.RowState == DataRowState.Modified)
                {
                    lsSinhVienMonHoc_ID.Add(ConvertInt(r["SinhVienMonHoc_ID"]));
                    lsIsDat.Add(ConvertBool(r["IsDat"]));
                }
            }
            return wSVMH.UpdateListDat(lsSinhVienMonHoc_ID.ToArray(), lsIsDat.ToArray());
        }
        #endregion

        #region Common update field
        public void LoadDataCommonUpdate(int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID, string TableUpdate, string ColumnUpdate, string ColumnLink, string Column_ID, string Filter)
        {
            List<string> Ls = new List<string>();
            Ls.Add(ColumnUpdate);
            LoadDataCommonUpdate(Khoa_ID, NganhHoc_ID, Lop_ID, KhoaHoc_ID, TableUpdate, Ls, ColumnLink, Column_ID, false, Filter);
        }
        private void LoadDataCommonUpdate(int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID, string TableUpdate, List<string> LsColumnUpdate, string ColumnLink, string Column_ID, bool IsInnerJoin, string Filter)
        {

            Table = dao.SearchDataCommonUpdate(Khoa_ID, NganhHoc_ID, Lop_ID, KhoaHoc_ID, TableUpdate, LsColumnUpdate.ToArray(), ColumnLink, Column_ID, IsInnerJoin, SinhVienObject, Filter).Tables[0];
            AddIndexToTable();
            AddTenGioiTinh(Table);
        }
        public DataTable LoadDataTableLookUp(string TableLookUp, string ValueMember, string DisplayMember)
        {
            return dao.SelectDataLookUpTable(TableLookUp, ValueMember, DisplayMember).Tables[0];
        }
        public void SaveDataCommon(string TableUpdate, string ColumnUpdate, string ColumnLink, string Column_ID, Dictionary<string, object> dicParams)
        {
            string typeName = "eTMS.Controllers." + TableUpdate + "Service." + TableUpdate;
            Type typeService = System.Reflection.Assembly.GetExecutingAssembly().GetType(typeName + "ServiceClient");
            Type typeObject = System.Reflection.Assembly.GetExecutingAssembly().GetType(typeName + "Entity");
            object obj = typeObject.GetConstructor(new Type[0]).Invoke(new object[0]);
            object objservice = typeService.GetConstructor(new Type[0]).Invoke(new object[0]);
            PropertyInfo propertyUpdate = null;
            PropertyInfo propertyLink = null;
            Dictionary<PropertyInfo, object> dicPropertiesValues = new Dictionary<PropertyInfo, object>();
            foreach (PropertyInfo info in typeObject.GetProperties())
            {
                if (info.CanWrite)
                {
                    if (info.PropertyType.Equals(typeof(int)) || info.PropertyType.Equals(typeof(Int64)) || info.PropertyType.Equals(typeof(double)))
                    {
                        info.SetValue(obj, 0, null);
                    }
                    else if (info.PropertyType.Equals(typeof(byte)) || info.PropertyType.Equals(typeof(sbyte)))
                    {
                        info.SetValue(obj, (byte)0, null);
                    }
                    else if (info.PropertyType.Equals(typeof(decimal)))
                    {
                        info.SetValue(obj, 0M, null);
                    }
                    else if (info.PropertyType.Equals(typeof(Single)))
                    {
                        info.SetValue(obj, 0f, null);
                    }
                    else if (info.PropertyType.Equals(typeof(DateTime)))
                    {
                        info.SetValue(obj, DateTime.Now, null);
                    }
                    else if (info.PropertyType.Equals(typeof(string)))
                    {
                        info.SetValue(obj, "", null);
                    }
                    if (info.Name.ToLower() == ColumnUpdate.Trim().ToLower())
                    {
                        propertyUpdate = info;
                    }
                    if (info.Name.ToLower() == ColumnLink.Trim().ToLower())
                    {
                        propertyLink = info;
                    }
                    if (dicParams.ContainsKey(info.Name.ToLower()))
                    {
                        dicPropertiesValues.Add(info, dicParams[info.Name.ToLower()]);
                    }
                }
            }
            foreach (DataRow r in Table.Rows)
            {
                if (r.IsNull(Column_ID + "Alias"))
                {
                    try
                    {
                        propertyUpdate.SetValue(obj, r[ColumnUpdate], null);
                        propertyLink.SetValue(obj, r["SinhVien_ID"], null);


                        foreach (PropertyInfo info in dicPropertiesValues.Keys)
                        {
                            info.SetValue(obj, dicPropertiesValues[info], null);
                        }
                        if (!r.IsNull(ColumnUpdate))
                        {
                            typeService.GetMethod("Save").Invoke(objservice, new object[] { obj });
                        }
                    }
                    catch
                    {
                    }
                }
                else
                {
                    try
                    {
                        object value = typeService.GetMethod("Update" + ColumnUpdate).Invoke(objservice, new object[] { r[ColumnUpdate], r[Column_ID + "Alias"] });
                    }
                    catch
                    {
                    }
                }
            }
        }
        public void SetDefaultDataCommon(string ColumnUpdate, object value)
        {
            foreach (DataRow r in Table.Rows)
            {
                r[ColumnUpdate] = value;
            }
        }
        TinhDiemTongKetHocKy tinhDiemController;
        public void UpdateTinhDiemTongKetHocKy()
        {
            tinhDiemController.UpdateData();
        }
        public void TinhDiemTongKetHocKy(int KhoaHoc_ID, int Khoa_ID, int NganhHoc_ID, int Lop_ID, int HocKy_ID, int NamHoc_ID, int LoaiDiem_ID)
        {
            if (tinhDiemController == null)
                tinhDiemController = new TinhDiemTongKetHocKy();
            tinhDiemController.ClearParameter();
            tinhDiemController.AddParameter("HocKy_ID", HocKy_ID);
            tinhDiemController.AddParameter("KhoaHoc_ID", KhoaHoc_ID);
            tinhDiemController.AddParameter("Lop_ID", Lop_ID);
            tinhDiemController.AddParameter("NamHoc_ID", NamHoc_ID);
            tinhDiemController.AddParameter("Khoa_ID", Khoa_ID);
            tinhDiemController.AddParameter("NganhHoc_ID", NganhHoc_ID);
            tinhDiemController.AddParameter("LoaiDiem_ID", LoaiDiem_ID);
            List<int> listID = new List<int>();
            Dictionary<int, int> dic = new Dictionary<int, int>();
            foreach (DataRow row in Table.Rows)
            {
                listID.Add(ConvertInt(row["SinhVien_ID"]));
                dic.Add(listID[listID.Count - 1], ConvertInt(row["SinhVienHocKy_ID"]));
            }
            tinhDiemController.LoadData(listID, SinhVienObject, dic);
            tinhDiemController.DoProcess();
            if (!Table.Columns.Contains("DiemTongKet"))
            {
                Table.Columns.Add("DiemTongKet", typeof(double));
            }
            foreach (DataRow r in Table.Rows)
            {
                r["DiemTongKet"] = tinhDiemController.GetResult(ConvertInt(r["SinhVien_ID"]));
            }
        }
        #endregion

        #region Hoc Bong SinhVien
        public void SearchHocBongSinhVien(int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID, int NamHoc_ID, int HocKy_ID)
        {
            Table = dao.SearchDanhSachHocBong(Khoa_ID, NganhHoc_ID, Lop_ID, KhoaHoc_ID, SinhVienObject, NamHoc_ID, HocKy_ID).Tables[0];
            Table = SortByLastName("TenSinhVien", "Ho", "Ten", Table);
            AddIndexToTable();
            AddTenGioiTinh(Table);
        }
        public void LapDSHocBong(int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID, int CachTinh_ID, int SoChiTieu, decimal SoTien, int NamHoc_ID, int HocKy_ID)
        {
            string Filter = "NamHoc_ID=" + NamHoc_ID.ToString() + " AND HocKy_ID=" + HocKy_ID.ToString();
            List<string> lsColumns = new List<string>();
            lsColumns.Add("DiemTongKet");
            lsColumns.Add("DiemRenLuyen");
            Table = dao.SearchDataCommonUpdate(Khoa_ID, NganhHoc_ID, Lop_ID, KhoaHoc_ID, "SinhVienHocKy", lsColumns.ToArray(), "SinhVien_ID", "SinhVienHocKy_ID", true, SinhVienObject, Filter).Tables[0];
            Table.DefaultView.Sort = "DiemTongKet DESC,DiemRenLuyen DESC";
            Table = Table.DefaultView.ToTable();
            Table.Columns.Add("MucHocBong_ID", typeof(int));
            CachTinhHocBongController cachTinhHocBongController = new CachTinhHocBongController();
            cachTinhHocBongController.LoadObject(CachTinh_ID);
            cachTinhHocBongController.LoadChiTiet();
            bool isXetChiTieu = SoChiTieu > 0;
            bool isXetTaiChinh = SoTien > 0;
            foreach (DataRow row in Table.Rows)
            {
                row["MucHocBong_ID"] = 0;
            }
            for (int i = 0; i < Table.Rows.Count; i++)
            {
                DataRow row = Table.Rows[i];
                decimal diem = ConvertNumber(row["DiemTongKet"]);
                decimal diemRL = ConvertNumber(row["DiemRenLuyen"]);
                bool found = false;
                foreach (DataRow dataRow in cachTinhHocBongController.TableChiTiet.Rows)
                {
                    decimal canDuoi = ConvertNumber(dataRow["TuDiem"]);
                    decimal canTren = ConvertNumber(dataRow["DenDiem"]);
                    bool LayCanDuoi = ConvertBool(dataRow["LayCanDuoi"]);
                    bool LayCanTren = ConvertBool(dataRow["LayCanTren"]);
                    decimal canDuoiRL = ConvertNumber(dataRow["TuDiemRL"]);
                    decimal canTrenRL = ConvertNumber(dataRow["DenDiemRL"]);
                    bool LayCanDuoiRL = ConvertBool(dataRow["LayCanDuoiRL"]);
                    bool LayCanTrenRL = ConvertBool(dataRow["LayCanTrenRL"]);

                    if (((diem > canDuoi && diem < canTren) || (diem == canDuoi && LayCanDuoi) || (diem == canTren && LayCanTren))
                        && ((diemRL > canDuoiRL && diemRL < canTrenRL) || (diemRL == canDuoiRL && LayCanDuoiRL) || (diemRL == canTrenRL && LayCanTrenRL)))
                    {
                        if (isXetTaiChinh)
                        {
                            SoTien -= ConvertNumber(dataRow["SoTien"]);
                            if (SoTien < 0) break;
                        }
                        found = true;
                        row["MucHocBong_ID"] = dataRow["MucHocBong_ID"];
                        break;
                    }
                }

                if (isXetChiTieu)
                {
                    SoChiTieu--;
                    if (SoChiTieu <= 0) break;
                }
                if (!found) break;
            }


            AddIndexToTable();
            AddTenGioiTinh(Table);

        }
        public void SaveLoDanhSachHocBong(int NamHoc_ID, int HocKy_ID, int CachTinhHocBong_ID)
        {
            DanhSachHocBongController danhSachHocBongController = new DanhSachHocBongController();
            foreach (DataRow row in Table.Rows)
            {
                if (ConvertInt(row["MucHocBong_ID"]) > 0)
                {
                    danhSachHocBongController.ResetObject();
                    danhSachHocBongController.DanhSachHocBongObject.SinhVien_ID = ConvertInt(row["SinhVien_ID"]);
                    danhSachHocBongController.DanhSachHocBongObject.NamHoc_ID = NamHoc_ID;
                    danhSachHocBongController.DanhSachHocBongObject.HocKy_ID = HocKy_ID;
                    danhSachHocBongController.DanhSachHocBongObject.MucHocBong_ID = ConvertInt(row["MucHocBong_ID"]);
                    danhSachHocBongController.DanhSachHocBongObject.CachTinhHocBong_ID = CachTinhHocBong_ID;
                    danhSachHocBongController.AddNew();
                }
            }
        }
        #endregion

        #region Xet duyet
        public void LapDanhSachCamThi(List<int> LsLyDoCamThi_ID, int HocKy_ID, int NamHoc_ID, int MonHoc_ID, int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID)
        {
            MonHocController.LoadAllMonHoc(false);
            SearchSinhVienHocMonHoc(NamHoc_ID, HocKy_ID, MonHoc_ID, Lop_ID);
            Table.Columns.Add("LyDoCamThi_ID");
            if (LsLyDoCamThi_ID.Count > 0)
            {
                List<string> lsID = new List<string>();
                CachChonSinhVienCamThiService.CachChonSinhVienCamThiServiceClient w = new eTMS.Controllers.CachChonSinhVienCamThiService.CachChonSinhVienCamThiServiceClient();
                List<CachChonSinhVienCamThiService.CachChonSinhVienCamThiEntity> lsCachLay = new List<eTMS.Controllers.CachChonSinhVienCamThiService.CachChonSinhVienCamThiEntity>(w.SelectAllFilter("LyDoCamThi_ID IN(" + string.Join(",", LsLyDoCamThi_ID.ConvertAll<string>(Convert.ToString).ToArray()) + ")"));
                foreach (CachChonSinhVienCamThiService.CachChonSinhVienCamThiEntity o in lsCachLay)
                {
                    string code = @"namespace eTMS.Controllers.Maths{
                                    using System.Collections.Generic;
                                    using System;
                                    using System.Data;
                                    using eTMS.Controllers.DiemMonHocService;
                                    using eTMS.Controllers.GiayToSinhVienService;
                                    using eTMS.Controllers.KhoaHocService;
                                    using eTMS.Controllers.LopService;
                                    using eTMS.Controllers.LopSinhVienService;
                                    using eTMS.Controllers.LyLichSinhVienService;
                                    using eTMS.Controllers.NganhHocService;
                                    using eTMS.Controllers.PhanNganhSinhVienService;
                                    using eTMS.Controllers.SinhVienHocKyService;
                                    using eTMS.Controllers.SinhVienLopTinChiService;
                                    using eTMS.Controllers.SinhVienMonHocService;
                                    using eTMS.Controllers.SinhVienService;
                                    using eTMS.Controllers.SoCoSinhVienChiTietService;
                                    using eTMS.Controllers.SoCoSinhVienService;
                                    using eTMS.Controllers.SoNoSinhVienChiTietService;
                                    using eTMS.Controllers.SoNoSinhVienService;
                                    using eTMS.Controllers.LoaiDiemService;
                                    public class ChonSinhVienCamThi" + o.LyDoCamThi_ID.ToString() + @" : SinhVienProcessData,ISinhVienProcess{
                                    " + o.NoiDung + @"
                                    }
                                }";
                    Assembly assembly = RuntimeCode.GetAssemblyFromCode(code);
                    Type type = assembly.GetType("eTMS.Controllers.Maths.ChonSinhVienCamThi" + o.LyDoCamThi_ID.ToString());
                    object objClass = type.GetConstructor(new Type[0]).Invoke(new Object[0]);
                    SinhVienProcessData objData = (SinhVienProcessData)objClass;
                    objData.AddParameter("HocKy_ID", HocKy_ID);
                    objData.AddParameter("NamHoc_ID", NamHoc_ID);
                    objData.AddParameter("MonHoc_ID", MonHoc_ID);
                    ISinhVienProcess objProcess = (ISinhVienProcess)objClass;
                    List<int> lsSinhVien_ID = new List<int>();
                    foreach (DataRow r in Table.Rows)
                    {
                        lsSinhVien_ID.Add(ConvertInt(r["SinhVien_ID"]));
                    }
                    objProcess.LoadData(lsSinhVien_ID);
                    objProcess.DoProcess();
                    for (int i = 0; i < Table.Rows.Count; i++)
                    {
                        if (ConvertBool(objProcess.GetResult(ConvertInt(Table.Rows[i]["SinhVien_ID"]))))
                        {
                            lsID.Add(Table.Rows[i]["SinhVien_ID"].ToString());
                            Table.Rows[i]["LyDoCamThi_ID"] = o.LyDoCamThi_ID;
                        }
                    }
                }
                if (lsID.Count > 0)
                {
                    DataRow[] rs = Table.Select("SinhVien_ID NOT IN(" + string.Join(",", lsID.ToArray()) + ")");
                    foreach (DataRow r in rs)
                        Table.Rows.Remove(r);
                }
                else
                {
                    Table.Rows.Clear();
                }
            }
            eTMS.Commons.Common.AddCheckedToTable(Table, true);
        }
        public void LoadDanhSachHocLai(int HocKy_ID, int NamHoc_ID, int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID)
        {
            Table = dao.SearchSinhVienHocLai(NamHoc_ID, HocKy_ID, Khoa_ID, NganhHoc_ID, Lop_ID, Khoa_ID, SinhVienObject).Tables[0];
            Table = SortByLastName("TenSinhVien", "Ho", "Ten", Table);
            AddTenGioiTinh(Table);
        }
        public void LapDanhSachHocLai(int KhoaHoc_ID, int HocKy_ID, int NamHoc_ID)
        {
            SinhVienHocLaiController controller = new SinhVienHocLaiController();
            NganhHocController.LoadAllNganhHoc(false);
            ChuongTrinhDaoTaoController ctrDaoTao = new ChuongTrinhDaoTaoController();

            for (int i = 0; i < NganhHocController.ListNganhHocStatic.Count; i++)
            {
                ctrDaoTao.LoadChuongTrinhDaoTaoSuDungByKhoaNganh(KhoaHoc_ID, NganhHocController.ListNganhHocStatic[i].NganhHoc_ID);
                controller.ClearParameter();
                Table = dao.SearchSinhVien_ID(-1, NganhHocController.ListNganhHocStatic[i].NganhHoc_ID, -1, KhoaHoc_ID, SinhVienObject).Tables[0];
                controller.AddParameter("HocKy_ID", HocKy_ID);
                controller.AddParameter("NamHoc_ID", NamHoc_ID);
                controller.AddParameter("KhoaHoc_ID", KhoaHoc_ID);
                controller.AddParameter("ChuongTrinhDaoTao_ID", ctrDaoTao.ChuongTrinhDaoTaoObject.ChuongTrinhDaoTao_ID);

                List<int> lsSinhVienID = new List<int>();
                foreach (DataRow row in Table.Rows)
                {
                    lsSinhVienID.Add(ConvertInt(row["SinhVien_ID"]));
                }
                controller.LoadData(lsSinhVienID);
                controller.DoProcess();
                controller.UpdateData();
            }
        }
        #endregion

        #region Tai khoan
        public void SearchTaiKhoanSinhVien(List<int> lsSinhVien_ID)
        {
            Table = dao.SearchSinhVienUsers(lsSinhVien_ID.ToArray()).Tables[0];
        }
        public void CreateUsers(int Role_ID)
        {
            SystemControllers.UsersController usersController = new UsersController();
            UserRoleController roleController = new UserRoleController();
            foreach (DataRow row in Table.Rows)
            {
                usersController.ResetObject();
                usersController.UsersObject = usersController.MapDataRowToObject(row);
                if (usersController.UsersObject.User_ID == 0)
                {
                    usersController.UsersObject.UserType = 2;
                    usersController.AddNew();
                    roleController.ResetObject();
                    roleController.UserRoleObject.Role_ID = Role_ID;
                    roleController.UserRoleObject.User_ID = usersController.UsersObject.User_ID;
                    roleController.AddNew();
                }
                else
                {
                    usersController.Update();
                }
            }
        }
        #endregion

        #region Lop hoc phan
        public void SearchSinhVienLopTinChi(List<int> LsLopTinChi_ID)
        {
            Table = dao.SearchSinhVienLopHocPhan(-1, -1, -1, -1, LsLopTinChi_ID.ToArray(), SinhVienObject).Tables[0];
            Table.Columns.Add("TenTrangThaiDangKy");
            foreach (DataRow row in Table.Rows)
            {
                int trt = ConvertInt(row["TrangThaiDangKy"]);
                if (trt == eTMS.Commons.Constants.TRANGTHAI_DANGKYLOP_MOITAO)
                {
                    row["TenTrangThaiDangKy"] = "Mới đăng ký";
                }
                else if (trt == eTMS.Commons.Constants.TRANGTHAI_DANGKYLOP_DUYET)
                {
                    row["TenTrangThaiDangKy"] = "Đã duyệt";
                }
                else if (trt == eTMS.Commons.Constants.TRANGTHAI_DANGKYLOP_KHONGDUYET)
                {
                    row["TenTrangThaiDangKy"] = "Không duyệt";
                }
            }
            Table = SortByLastName("TenSinhVien", "Ho", "Ten", Table);
            AddTenGioiTinh(Table);
            AddIndexToTable();
        }

        #endregion

        #region Thong ke
        public DataSet ThongKeSinhVienTheoGioiTinh(int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID)
        {
            DataSet ds = dao.ThongKeSinhVien(Khoa_ID, NganhHoc_ID, Lop_ID, KhoaHoc_ID, SinhVienObject, "", "GioiTinh", "", false);
            AddTenGioiTinh(ds.Tables[0]);
            ds.Tables[0].Columns["TenGioiTinh"].ColumnName = "TenTieuChi";
            AddColumnTyLe(ds.Tables[0]);
            return ds;
        }
        public DataSet ThongKeSinhVienTheoDoiTuongTroCap(int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID)
        {
            DataSet ds = dao.ThongKeSinhVien(Khoa_ID, NganhHoc_ID, Lop_ID, KhoaHoc_ID, SinhVienObject, "DoiTuongTroCap", "LoaiDoiTuongTroCap_ID", "SinhVien_ID", false);
            ds.Tables[0].Columns.Add("TenTieuChi");
            LoaiDoiTuongTroCapController.LoadAllLoaiDoiTuongTroCap(false);
            LoaiDoiTuongTroCapController ctrl = new LoaiDoiTuongTroCapController();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                LoaiDoiTuongTroCapService.LoaiDoiTuongTroCapEntity oFind = ctrl.FindLoaiDoiTuongTroCap(ConvertInt(r["LoaiDoiTuongTroCap_ID"]));
                if (oFind != null)
                    r["TenTieuChi"] = oFind.TenLoaiDoiTuong;
                else
                    r["TenTieuChi"] = eTMS.Commons.Constants.APP_NOTSET;
            }
            AddColumnTyLe(ds.Tables[0]);
            return ds;
        }

        public DataSet ThongKeSinhVienTheoDanToc(int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID)
        {
            DataSet ds = dao.ThongKeSinhVien(Khoa_ID, NganhHoc_ID, Lop_ID, KhoaHoc_ID, SinhVienObject, "LyLichSinhVien", "DanToc_ID", "SinhVien_ID", false);
            ds.Tables[0].Columns.Add("TenTieuChi");
            DanTocController ctrl = new DanTocController();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                DanTocEntity oDanToc = ctrl.FindDanToc(ConvertInt(r["DanToc_ID"]));
                if (oDanToc != null)
                    r["TenTieuChi"] = oDanToc.TenDanToc;
                else
                    r["TenTieuChi"] = eTMS.Commons.Constants.APP_NOTSET;
            }
            AddColumnTyLe(ds.Tables[0]);
            return ds;
        }
        public DataSet ThongKeSinhVienTheoTonGiao(int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID)
        {
            DataSet ds = dao.ThongKeSinhVien(Khoa_ID, NganhHoc_ID, Lop_ID, KhoaHoc_ID, SinhVienObject, "LyLichSinhVien", "TonGiao_ID", "SinhVien_ID", false);
            TonGiaoController.LoadAllTonGiao(false);
            ds.Tables[0].Columns.Add("TenTieuChi");
            TonGiaoController ctrl = new TonGiaoController();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                TonGiaoEntity oTonGiao = ctrl.FindTonGiao(ConvertInt(r["TonGiao_ID"]));
                if (oTonGiao != null)
                    r["TenTieuChi"] = oTonGiao.TenTonGiao;
                else
                    r["TenTieuChi"] = eTMS.Commons.Constants.APP_NOTSET;
            }
            AddColumnTyLe(ds.Tables[0]);
            return ds;
        }
        public DataSet ThongKeSinhVienTheoKhuVuc(int Khoa_ID, int NganhHoc_ID, int Lop_ID, int KhoaHoc_ID)
        {
            DataSet ds = dao.ThongKeSinhVien(Khoa_ID, NganhHoc_ID, Lop_ID, KhoaHoc_ID, SinhVienObject, "LyLichSinhVien", "KhuVuc_ID", "SinhVien_ID", false);
            KhuVucController.LoadAllKhuVuc(false);
            ds.Tables[0].Columns.Add("TenTieuChi");
            KhuVucController ctrl = new KhuVucController();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                KhuVucEntity oKhuVuc = ctrl.FindKhuVuc(ConvertInt(r["KhuVuc_ID"]));
                if (oKhuVuc != null)
                    r["TenTieuChi"] = oKhuVuc.TenKhuVuc;
                else
                    r["TenTieuChi"] = eTMS.Commons.Constants.APP_NOTSET;
            }
            AddColumnTyLe(ds.Tables[0]);
            return ds;
        }
        private void AddColumnTyLe(DataTable tblData)
        {
            tblData.Columns.Add("TYLE", typeof(decimal));
            decimal total = 0;
            foreach (DataRow r in tblData.Rows)
            {
                total += ConvertNumber(r["SoLuong"]);
            }
            if (total > 0)
            {
                foreach (DataRow r in tblData.Rows)
                {
                    r["TyLe"] = ConvertNumber(r["SoLuong"]) / total;
                }
            }
        }
        #endregion


    }


    public interface ISinhVienProcess
    {
        void LoadData(List<int> LsSinhVien_ID, params object[] others);
        void DoProcess();
        object GetResult(int SinhVien_ID, params object[] others);
        void UpdateData();
    }
    public class SinhVienProcessData : CommonController
    {
        protected DataTable _Table;
        private Dictionary<string, object> dic = new Dictionary<string, object>();
        public DataTable Table
        {
            get
            {
                return _Table;
            }
            set
            {
                _Table = value;
            }
        }
        public void AddParameter(string key, object value)
        {
            key = key.ToUpper();
            if (!dic.ContainsKey(key))
            {
                dic.Add(key, value);
            }
            else
            {
                dic[key] = value;
            }

        }
        public void RemoveParameter(string key)
        {
            key = key.ToUpper();
            dic.Remove(key);
        }
        public void ClearParameter()
        {
            dic.Clear();
        }
        public object GetParameter(string key)
        {
            key = key.ToUpper();
            if (!dic.ContainsKey(key)) return null;
            return dic[key];
        }
        public int GetIntParamemter(string key)
        {
            return ConvertInt(GetParameter(key));
        }
    }
    public class TinhDiemTongKetHocKy : SinhVienProcessData, ISinhVienProcess
    {
        #region ISinhVienProcess Members
        private Dictionary<int, double[]> diemTongKet = new Dictionary<int, double[]>();
        public void LoadData(List<int> LsSinhVien_ID, params object[] others)
        {
            diemTongKet.Clear();
            Dictionary<int, int> dic_ID = (Dictionary<int, int>)others[1];
            SinhVienService.SinhVienServiceClient w = new SinhVienService.SinhVienServiceClient();
            _Table = w.SearchSinhVienWithDiemMonHoc(false, true, true, false, -1, GetIntParamemter("LoaiDiem_ID"), -1, GetIntParamemter("HocKy_ID"), GetIntParamemter("NamHoc_ID"), GetIntParamemter("Khoa_ID"), GetIntParamemter("NganhHoc_ID"), GetIntParamemter("Lop_ID"), GetIntParamemter("KhoaHoc_ID"), (SinhVienEntity)others[0], "SinhVien.SinhVien_ID IN(" + string.Join(",", LsSinhVien_ID.ConvertAll<string>(Convert.ToString).ToArray()) + ")").Tables[0];
            foreach (DataRow r in _Table.Rows)
            {
                int sinhVien_ID = ConvertInt(r["SinhVien_ID"]);
                if (!diemTongKet.ContainsKey(sinhVien_ID))
                {
                    diemTongKet.Add(sinhVien_ID, new double[2]);
                    diemTongKet[sinhVien_ID][0] = 0;
                    diemTongKet[sinhVien_ID][1] = dic_ID[sinhVien_ID];
                }
            }
        }
        public void DoProcess()
        {
            ChuongTrinhDaoTaoController chuongTrinhDaoTaoController = new ChuongTrinhDaoTaoController();
            Dictionary<int, List<int>> dicKhoaNganh = new Dictionary<int, List<int>>();
            KhoaHocController khoaHocController = new KhoaHocController();
            HocKyController hocKyController = new HocKyController();
            hocKyController.LoadObject(GetIntParamemter("HocKy_ID"));
            NamHocController namHocController = new NamHocController();
            namHocController.LoadObject(GetIntParamemter("NamHoc_ID"));

            foreach (DataRow row in this._Table.Rows)
            {
                int khoaHoc_id = ConvertInt(row["KhoaHoc_ID"]);
                int nganhHoc_ID = ConvertInt(row["NganhHoc_ID"]);
                if (!dicKhoaNganh.ContainsKey(khoaHoc_id))
                {
                    dicKhoaNganh.Add(khoaHoc_id, new List<int>());
                }
                if (!dicKhoaNganh[khoaHoc_id].Contains(nganhHoc_ID))
                    dicKhoaNganh[khoaHoc_id].Add(nganhHoc_ID);
            }
            foreach (int KhoaHoc_ID in dicKhoaNganh.Keys)
            {
                khoaHocController.LoadObject(KhoaHoc_ID);
                int NamThu = GetNamThu(khoaHocController.KhoaHocObject.BatDau, namHocController.NamHocObject.NgayBatDau);
                foreach (int NganhHoc_ID in dicKhoaNganh[KhoaHoc_ID])
                {
                    chuongTrinhDaoTaoController.LoadChuongTrinhDaoTaoSuDungByKhoaNganh(KhoaHoc_ID, NganhHoc_ID);
                    chuongTrinhDaoTaoController.LoadChiTiet();
                    List<MonHocService.MonHocEntity> lsMonHoc = chuongTrinhDaoTaoController.LoadAllMonHocHocTheoHocKyNamThu(NamThu, hocKyController.HocKyObject.HocKy_ID, chuongTrinhDaoTaoController.ChuongTrinhDaoTaoObject.ChuongTrinhDaoTao_ID);
                    Dictionary<int, Dictionary<int, double>> dic = new Dictionary<int, Dictionary<int, double>>();
                    foreach (MonHocService.MonHocEntity monHocEntity in lsMonHoc)
                    {
                        DataRow[] rows = _Table.Select("KhoaHoc_ID=" + KhoaHoc_ID.ToString() + " AND NganhHoc_ID=" + NganhHoc_ID.ToString() + " AND MonHoc_ID=" + monHocEntity.MonHoc_ID.ToString());
                        foreach (DataRow r in rows)
                        {
                            int SinhVien_ID = ConvertInt(r["SinhVien_ID"]);
                            if (!dic.ContainsKey(SinhVien_ID))
                            {
                                dic.Add(SinhVien_ID, new Dictionary<int, double>());
                            }
                            if (!dic[SinhVien_ID].ContainsKey(monHocEntity.MonHoc_ID))
                            {
                                dic[SinhVien_ID].Add(monHocEntity.MonHoc_ID, ConvertDouble(r["Diem"]));
                            }
                            else
                            {
                                dic[SinhVien_ID][monHocEntity.MonHoc_ID] = ConvertDouble(r["Diem"]);
                            }
                        }
                    }
                    foreach (int sinhVien_ID in dic.Keys)
                    {
                        double total = 0;
                        double totalhocPhan = 0;
                        foreach (MonHocService.MonHocEntity monHocEntity in lsMonHoc)
                        {
                            double diem = 0;
                            if (dic[sinhVien_ID].ContainsKey(monHocEntity.MonHoc_ID))
                            {
                                diem = dic[sinhVien_ID][monHocEntity.MonHoc_ID];
                            }

                            total += diem * (double)monHocEntity.SoHocPhan;
                            totalhocPhan += (double)monHocEntity.SoHocPhan;
                        }
                        diemTongKet[sinhVien_ID][0] = Math.Round(total / totalhocPhan, 2);

                    }
                }
            }
        }
        public object GetResult(int SinhVien_ID, params object[] others)
        {
            if (!diemTongKet.ContainsKey(SinhVien_ID)) return 0;
            return diemTongKet[SinhVien_ID][0];
        }
        public void UpdateData()
        {
            SinhVienHocKyService.SinhVienHocKyServiceClient w = new eTMS.Controllers.SinhVienHocKyService.SinhVienHocKyServiceClient();
            List<SinhVienHocKyService.SinhVienHocKyEntity> lsSinhVienHocKy = new List<eTMS.Controllers.SinhVienHocKyService.SinhVienHocKyEntity>();
            List<int> lsSinhVienHocKy_ID = new List<int>();
            List<decimal> lsDiemTongKet = new List<decimal>();
            foreach (int id in diemTongKet.Keys)
            {
                if (diemTongKet[id][1] > 0)
                {
                    lsSinhVienHocKy_ID.Add((int)diemTongKet[id][1]);
                    lsDiemTongKet.Add((decimal)diemTongKet[id][0]);
                }
                else
                {
                    SinhVienHocKyService.SinhVienHocKyEntity o = new eTMS.Controllers.SinhVienHocKyService.SinhVienHocKyEntity();
                    o.DiemRenLuyen = 0;
                    o.DiemTongKet = (decimal)diemTongKet[id][0];
                    o.HocKy_ID = GetIntParamemter("HocKy_ID");
                    o.NamHoc_ID = GetIntParamemter("NamHoc_ID");
                    o.NamThu = 1;
                    o.SinhVien_ID = id;
                    o.ThangDanhGia_ID = 0;
                    o.MaXepLoaiTongKet = "";
                    o.XepLoaiRenLuyen_ID = 0;
                    lsSinhVienHocKy.Add(o);
                }
            }
            List<SinhVienHocKyService.SinhVienHocKyEntity> lsCurrent = new List<eTMS.Controllers.SinhVienHocKyService.SinhVienHocKyEntity>();
            for (int i = 0; i < lsSinhVienHocKy.Count; i++)
            {
                lsCurrent.Add(lsSinhVienHocKy[i]);
                if (lsCurrent.Count >= 1000)
                {
                    lsCurrent = new List<SinhVienHocKyEntity>(w.SaveListRet(lsCurrent.ToArray()));
                    foreach (SinhVienHocKyEntity sinhVienHocKyEntity in lsCurrent)
                    {
                        diemTongKet[sinhVienHocKyEntity.SinhVien_ID][1] = sinhVienHocKyEntity.SinhVienHocKy_ID;
                    }
                    lsCurrent = new List<eTMS.Controllers.SinhVienHocKyService.SinhVienHocKyEntity>();
                }
            }
            lsCurrent = new List<SinhVienHocKyEntity>(w.SaveListRet(lsCurrent.ToArray()));
            foreach (SinhVienHocKyEntity sinhVienHocKyEntity in lsCurrent)
            {
                diemTongKet[sinhVienHocKyEntity.SinhVien_ID][1] = sinhVienHocKyEntity.SinhVienHocKy_ID;
            }
            w.UpdateListDiemTongKet(lsSinhVienHocKy_ID.ToArray(), lsDiemTongKet.ToArray());
        }
        #endregion
    }
    //public class ChonSinhVienCamThi : SinhVienProcessData, ISinhVienProcess
    //{
    //    #region ISinhVienProcess Members
    //    private DataTable tblData = new DataTable();
    //    private int SoTietHoc = 0;
    //    public void LoadData(List<int> LsSinhVien_ID, params object[] others)
    //    {
    //        int MonHoc_ID = GetIntParamemter("MonHoc_ID");
    //        int NamHoc_ID = GetIntParamemter("NamHoc_ID");
    //        int HocKy_ID = GetIntParamemter("HocKy_ID");
    //        double PhanTramLanNghi = GetDoubleConfig("PHANTRAM_TIETNGHI_TOIDA");
    //        SoTietHoc = (int)Math.Round(PhanTramLanNghi * (double)MonHocController.FindMonHoc(MonHoc_ID).SoTietHoc / (double)100);
    //        DiemDanhSinhVienService.DiemDanhSinhVienServiceClient wDiemDanh = new eTMS.Controllers.DiemDanhSinhVienService.DiemDanhSinhVienServiceClient();
    //        tblData = wDiemDanh.SelectAllDSSinhVienSoLanNghiHoc(HocKy_ID, NamHoc_ID, MonHoc_ID, "").Tables[0];
    //    }
    //    public void DoProcess()
    //    {

    //    }
    //    public object GetResult(int SinhVien_ID, params object[] others)
    //    {
    //        DataRow[] rs = tblData.Select("SinhVien_ID=" + SinhVien_ID.ToString());
    //        if (rs.Length > 0)
    //        {
    //            int soLan=ConvertInt(rs[0]["SoLanNghi"]);
    //            return soLan > SoTietHoc;
    //        }
    //        return false;
    //    }
    //    public void UpdateData()
    //    {

    //    }
    //    #endregion
    //}
    public class SinhVienHocLaiController : SinhVienProcessData, ISinhVienProcess
    {
        #region ISinhVienProcess Members
        private DataTable tblSoMon;
        public void LoadData(List<int> LsSinhVien_ID, params object[] others)
        {
            int NamHoc_ID = GetIntParamemter("NamHoc_ID");
            int HocKy_ID = GetIntParamemter("HocKy_ID");
            int ChuongTrinhDaoTao_ID = GetIntParamemter("ChuongTrinhDaoTao_ID");
            DanhSachDeNghiLuuBanService.DanhSachDeNghiLuuBanServiceClient wDS = new eTMS.Controllers.DanhSachDeNghiLuuBanService.DanhSachDeNghiLuuBanServiceClient();
            this.Table = wDS.SelectAllDSToCalculate(LsSinhVien_ID.ToArray(), NamHoc_ID, HocKy_ID, ChuongTrinhDaoTao_ID).Tables[0];
            tblSoMon = wDS.SelectDSSoMonHocByChuongTrinhDaoTao(NamHoc_ID, HocKy_ID, ChuongTrinhDaoTao_ID).Tables[0];
            this.Table.Columns.Add("IsHocLai", typeof(bool));
        }

        public void DoProcess()
        {
            decimal percentPass = (decimal)GetDoubleConfig("PERCENT_SBJWEIGHT");
            decimal numOfWeight = ConvertNumber(tblSoMon.Rows[0][1]);
            foreach (DataRow row in this.Table.Rows)
            {
                //Ko dat so phan tram hoc phan
                decimal weight = ConvertNumber(row["SoHPKoDat"]);
                if (100 * weight / numOfWeight > percentPass)
                {
                    row["IsHocLai"] = true;
                }
                else
                {
                    row["IsHocLai"] = false;
                }
            }
        }

        public object GetResult(int SinhVien_ID, params object[] others)
        {
            return null;
        }

        public void UpdateData()
        {
            int NamHoc_ID = GetIntParamemter("NamHoc_ID");
            int HocKy_ID = GetIntParamemter("HocKy_ID");
            DanhSachDeNghiLuuBanService.DanhSachDeNghiLuuBanServiceClient wDS = new eTMS.Controllers.DanhSachDeNghiLuuBanService.DanhSachDeNghiLuuBanServiceClient();
            DanhSachDeNghiLuuBanService.DanhSachDeNghiLuuBanEntity entity;
            List<DanhSachDeNghiLuuBanService.DanhSachDeNghiLuuBanEntity> lsSave = new List<eTMS.Controllers.DanhSachDeNghiLuuBanService.DanhSachDeNghiLuuBanEntity>();
            foreach (DataRow row in this.Table.Rows)
            {
                if (ConvertBool(row["IsHocLai"]))
                {
                    int SinhVien_ID = ConvertInt(row["SinhVien_ID"]);
                    entity = new eTMS.Controllers.DanhSachDeNghiLuuBanService.DanhSachDeNghiLuuBanEntity();
                    entity.HocKy_ID = HocKy_ID;
                    entity.NamHoc_ID = NamHoc_ID;
                    lsSave.Add(entity);
                }
            }
            wDS.SaveList(lsSave.ToArray());
        }

        #endregion
    }

}
